using SmartCommunity.WebApi.Services.System;
using Microsoft.AspNetCore.Mvc;

namespace SmartCommunity.WebApi.Controllers.System
{
    /// <summary>
    /// API接口：数据字典
    /// </summary>
    [ApiController]
    [Route("api/system/[controller]")]
    public class DictController : BaseController
    {
        #region 构造函数
        private readonly DictService dictService;
        public DictController(DictService dictService)
        {
            this.dictService = dictService;
        }
        #endregion

        #region 查询导出 
        /// <summary>
        /// 分页查询
        /// </summary>
        [HttpGet]
        public async Task<PageResult<DictDto>> GetList([FromQuery] DictQuery query, [FromQuery] Pager pager)
        {
            return await dictService.FindPagedList(query, pager, CurrentUser);
        }

        /// <summary>
        /// 通过主键查询
        /// </summary>
        /// <param name="id">主键ID</param>
        [HttpGet("{id}")]
        public async Task<DictDto> GetOne(int id)
        {
            return await dictService.FindDictById(id);
        }

        /// <summary>
        /// 导出excel
        /// </summary>
        [HttpGet("export")]
        public async Task<ActionResult> ExportExcel([FromQuery] DictQuery query)
        {
            var lstData = await dictService.FindExportList(query, CurrentUser);
            var exporter = new ExcelExporter();
            exporter.AddTitleRow(0, "字典类别", "字典编码", "字典名称", "字典值", "字典状态", "排序号", "备注说明", "创建人", "创建时间");
            int rowIndex = 1;
            foreach (var dto in lstData)
            {
                exporter.AddDataRow(rowIndex++, 0, $"{dto.DictName}({dto.DictType})", dto.DictCode, dto.DictLabel, dto.DictValue, dto.Disabled ? "作废" : "正常", dto.SortNo ?? 0, dto.Remark, dto.CreatedBy, dto.CreatedTime);
            }
            var bytes = exporter.GetExcelData();
            return File(bytes, CONTENT_TYPE_EXCEL, $"数据字典导出{DateTime.Now:yyyyMMddHHmmss}.xlsx");
        }
        #endregion

        #region 增 删 改
        /// <summary>
        /// 新增数据字典
        /// </summary>
        [HttpPost]
        [Log("新增数据字典", OperateType.Create, false, true)]
        public async Task<ActionResult<DictDto>> Create([FromBody] DictDto dto)
        {
            var dict = await dictService.CreateDict(dto, CurrentUser);
            return CreatedAtAction("GetOne", new { id = dict.DictId }, dict);
        }

        /// <summary>
        /// 修改数据字典
        /// </summary>
        [HttpPut("{id}")]
        [Log("修改数据字典", OperateType.Update)]
        public async Task<ActionResult> Update(int id, [FromBody] DictDto dto)
        {
            if (id != dto.DictId)
            {
                return WarningMessage("要更新的数据和ID不匹配");
            }
            await dictService.UpdateDict(dto, CurrentUser);
            return NoContent();
        }

        /// <summary>
        /// 删除数据字典
        /// </summary>
        [HttpDelete]
        [Log("删除数据字典", OperateType.Delete)]
        public async Task<ActionResult> Delete([FromQuery] int[] ids)
        {
            await dictService.DeleteByIds(ids, CurrentUser);
            return NoContent();
        }
        #endregion
    }
}
